package com.zlq.add.Thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用ReentrantLock实现同步
 * lock()方法：上锁
 * unlock()方法：释放锁
 */

public class TestReentrantLock {
    public static void main(String[] args) {
        MyReentrantLock myReentrantLock = new MyReentrantLock();
        Thread thread1 = new Thread(myReentrantLock);
        Thread thread2 = new Thread(myReentrantLock);
        Thread thread3 = new Thread(myReentrantLock);
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

class MyReentrantLock implements Runnable{
    //向上转型
    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
        //上锁
        lock.lock();
        for (int i = 0; i <5; i++) {
            System.out.println("当前线程名："+Thread.currentThread().getName()+",i="+i);
        }
        //释放锁
        lock.unlock();

    }
}
